angular.module('goods-search', [])

.controller 'GoodsSearchCtrl', ($ionicActionSheet,$scope, $state, $http, $ionicHistory,showToast, $stateParams, wfCache,$ionicViewSwitcher,$ionicPopup,goodsInfo,userData,$ionicScrollDelegate) ->
    $scope.currentId = $stateParams.currentItemId
    console.log $scope.currentId
    $scope.canLoad = false
    page = 1
    pagesize = 5
    $scope.goodsResp = []
    $scope.getWarehouse = ->
        if $scope.currentId == '0'
            $scope.warehouse = 1
        else if $scope.currentId == '1'
            $scope.warehouse = 0
    $scope.getStype = ->
        if $scope.currentId == '10'
            $scope.stype = 0
        else
            $scope.stype = 1
    requestData = (isLoadMore)->
            if !isLoadMore
                page = 1
                $scope.goodsResp = []
            else
                page++
            $scope.getWarehouse()
            $scope.getStype()
            $http.get('/utf-8data/',params:{action:'productlist',userid:userData.getToken(),page:page,pagesize:pagesize,sortid:'0',keywords:$scope.searchInfo.searchWords,warehouse:$scope.warehouse,stype:$scope.stype})
            .success (resp) ->
                if resp?.status == '0'
                    if parseInt(resp.allpage) < parseInt(page)
                        $scope.canLoad = false
                    else
                        $scope.canLoad = true

                    $scope.goodsResp = $scope.goodsResp.concat resp.body
                    $scope.goodsList = _.map $scope.goodsResp,(item)->
                        goods = {
                            id:item.id
                            img:item.img
                            txt1:item.title
                            txt2:"销售价:￥#{item.price}"
                            advice:item.advice
                            defaultprice:item.defaultprice
                            title:item.title
                        }
                    if $scope.checkContent and $scope.goodsList?.length==0
                        $scope.nocontent=true
                    else
                        $scope.nocontent=false
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
            .finally ->
                $scope.$broadcast 'scroll.infiniteScrollComplete'
    _.extend $scope,
        searchInfo:
            searchWords:undefined
        loadMore: ->
            requestData(true)
        goSearch:()->
            console.log $scope.searchInfo.searchWords
            if $scope.searchInfo.searchWords == ''
                $scope.goodsList = []
            else
                $ionicScrollDelegate.scrollTop()
                requestData(false)
        goBack: ->
            $ionicViewSwitcher.nextDirection('back')
            $ionicHistory.goBack()

        pick: (i) ->
            console.log 'pick',i
            if $scope.currentId == '0'
                goodsInfo.isOnLine = true
                $state.go 'tab.goods-detail', goodId: $scope.goodsList[i].id
            else if $scope.currentId == '1'
                goodsInfo.isOnLine = false
                $state.go 'tab.goods-detail', goodId: $scope.goodsList[i].id
            else
                $state.go 'tab.market-detail',{item: $scope.goodsList[i]}
        clear: ->
            $scope.searchInfo.searchWords = undefined

    $scope.$on '$ionicView.enter', ->
        $scope.checkContent=true


